﻿#ifndef FRAMELESSWIDGET_H
#define FRAMELESSWIDGET_H

#include <QMainWindow>
#include <QWidget>
#include <QPropertyAnimation>
#include <QMap>
QT_BEGIN_NAMESPACE
class QVBoxLayout;
class QHBoxLayout;
class QFrame;
class QPushButton;
QT_END_NAMESPACE

#define USE_LOCAL_PAINT





class FramelessWidget : public QWidget
{
    Q_OBJECT

    enum WindowState {
        LeftHalfWindow,
        RightHalfWindow,
        MaximumWindow,
        DefaultWindow
    };

public:
    explicit FramelessWidget(QWidget *parent = nullptr);
    ~FramelessWidget();

    void setCenterWidget(QWidget *widget);
    void setZeroMargin(bool flag);
    void setMoveEnable(bool moveEnable);
    void setResizeEnable(bool resizeEnable);

signals:
    void sigClose();


protected:
    bool eventFilter(QObject *watched, QEvent *event) override;
#ifdef USE_LOCAL_PAINT
    void paintEvent(QPaintEvent *event) override;
#endif

private:
    int padding;                    //边距
    bool moveEnable;                //可移动
    bool resizeEnable;              //可拉伸

    bool pressed;                   //鼠标按下
    bool pressedLeft;               //鼠标按下左侧
    bool pressedRight;              //鼠标按下右侧
    bool pressedTop;                //鼠标按下上侧
    bool pressedBottom;             //鼠标按下下侧
    bool pressedLeftTop;            //鼠标按下左上侧
    bool pressedRightTop;           //鼠标按下右上侧
    bool pressedLeftBottom;         //鼠标按下左下侧
    bool pressedRightBottom;        //鼠标按下右下侧

    int rectX, rectY, rectW, rectH; //窗体坐标+宽高
    QPoint lastPos;                 //鼠标按下处坐标

    QRect rectLeft;                 //左侧区域
    QRect rectRight;                //右侧区域
    QRect rectTop;                  //上侧区域
    QRect rectBottom;               //下侧区域
    QRect rectLeftTop;              //左上侧区域
    QRect rectRightTop;             //右上侧区域
    QRect rectLeftBottom;           //左下侧区域
    QRect rectRightBottom;          //右下侧区域

    QVBoxLayout *m_vBoxLayout;
    QFrame      *m_headerFrame;
    QHBoxLayout *m_hBoxLayout;
    QVBoxLayout *m_vBoxLayout1;

    QPushButton *btn_min;
    QPushButton *btn_max;
    QPushButton *btn_close;

    QRect m_lastRect;
//TODO.0
//    BaseWidget* m_baseWidget;
//    BubbleWidget* m_bubbleWidget;
    WindowState m_windowState;
    QWidget    *m_centerWidget;

    QMap<QPushButton *, QMenu *> btnMenuMap;
    QMenuBar *m_menuBar;
private:
    void setPadding(int padding);

private:
    void initUi();
    bool tryMoveWidget(QMouseEvent *event);

};

#endif // FRAMELESSWIDGET_H
